package fm.jiecao.jcvideoplayer_lib;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

public class MyPopuwindow implements PopuwindowAdapter.OnItemClickListener{
    private PopupWindow popupwindow;
    private onClickLinear ClickLinear;
    private int popupWidth;
    private int popupHeight;
    private PopuwindowAdapter speedPopuwindowAdapter;
    private TextView mTextView;
    private int popuwindowHeight;
    public void setOnClickLinear(onClickLinear ClickLinear) {
        this.ClickLinear = ClickLinear;
    }

    public void showSpeedPopuwindow(Context context,  List<Float> nameList, TextView view1) {
        this.mTextView=view1;
        if(speedPopuwindowAdapter!=null){
            showUp(view1,nameList);
            return;
        }
        popuwindowHeight=context.getResources().getDimensionPixelSize(R.dimen.dimen_26);
        LayoutInflater inflater=LayoutInflater.from(context);
        View parentView  = inflater.inflate(R.layout.speed_popuwindow_layout, null);
        RecyclerView listView=parentView.findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new
                LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        listView.setLayoutManager(layoutManager);
        LinearLayout lin=parentView.findViewById(R.id.lin);
        lin.getBackground().setAlpha(100);//0~255透明度值
        speedPopuwindowAdapter = new PopuwindowAdapter(context,nameList);
        listView.setAdapter(speedPopuwindowAdapter);
        //获取自身的长宽高
        parentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = parentView.getMeasuredHeight();
        popupWidth = parentView.getMeasuredWidth();
        int hight=listView.getHeight();
        popupwindow = new PopupWindow(parentView, view1.getWidth(), popuwindowHeight*nameList.size(), true);
        popupwindow.setFocusable(true);
        popupwindow.setOutsideTouchable(true);
        popupwindow.setBackgroundDrawable(new BitmapDrawable());
        popupwindow.setTouchable(true);
        showUp(view1,nameList);
        speedPopuwindowAdapter.setOnItemClickListener(this);
    }

    public void hidePopuWindow(){
        if(popupwindow!=null){
            popupwindow.dismiss();
        }
    }

    /**
     * 设置显示在v上方（以v的中心位置为开始位置）
     * @param v
     */
    public void showUp(View v,List<Float> nameList) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        popupwindow.dismiss();
        popupwindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - (popuwindowHeight*nameList.size()+20));
        //speedPopuwindowAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View v, float viewName, int position) {
        ClickLinear.callResult(viewName);
        mTextView.setText("倍速:"+viewName);
    }

    public interface onClickLinear {
        void callResult(float speed);
    }
}
